local gn_jieying = fk.CreateSkill {

  name = "joy__gn_jieying",

  tags = {  },

}



gn_jieying:addEffect(fk.DrawNCards, {
  name = "joy__gn_jieying",
  anim_type = "drawcard",
  events = {fk.DrawNCards, fk.EventPhaseStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(gn_jieying.name) then return false end
    if event == fk.TurnStart then
      return player == target and table.every(player.room.alive_players, function (p)
        return p:getMark("@@joy__jieying_camp") == 0 end)
    elseif event == fk.EventPhaseStart and target.phase ~= Player.Finish then
      return false
    end
    return target:getMark("@@joy__jieying_camp") > 0
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart and player == target then
      local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function (p)
        return p.id end), 1, 1, "#joy__gn_jieying-choose", gn_jieying.name, true)
      if #to > 0 then
        event:setCostData(self, to[1])
        return true
      end
      return false
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:addPlayerMark(player, "@@joy__jieying_camp")
    elseif event == fk.DrawNCards then
      data.n = data.n + 1
    elseif event == fk.EventPhaseStart then
      if player == target then
        local tar = room:getPlayerById(event:getCostData(self))
        room:setPlayerMark(player, "@@joy__jieying_camp", 0)
        room:addPlayerMark(tar, "@@joy__jieying_camp")
      else
        room:setPlayerMark(target, "@@joy__jieying_camp", 0)
        if not target:isKongcheng() then
          local dummy = Fk:cloneCard("dilu")
          dummy:addSubcards(target.player_cards[Player.Hand])
          room:obtainCard(player, dummy, false, fk.ReasonPrey)
        end
      end
    end
    return false
  end,
})
gn_jieying:addEffect(fk.EventPhaseStart, {
  name = "joy__gn_jieying",
  anim_type = "drawcard",
  events = {fk.DrawNCards, fk.EventPhaseStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(gn_jieying.name) then return false end
    if event == fk.TurnStart then
      return player == target and table.every(player.room.alive_players, function (p)
        return p:getMark("@@joy__jieying_camp") == 0 end)
    elseif event == fk.EventPhaseStart and target.phase ~= Player.Finish then
      return false
    end
    return target:getMark("@@joy__jieying_camp") > 0
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart and player == target then
      local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function (p)
        return p.id end), 1, 1, "#joy__gn_jieying-choose", gn_jieying.name, true)
      if #to > 0 then
        event:setCostData(self, to[1])
        return true
      end
      return false
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:addPlayerMark(player, "@@joy__jieying_camp")
    elseif event == fk.DrawNCards then
      data.n = data.n + 1
    elseif event == fk.EventPhaseStart then
      if player == target then
        local tar = room:getPlayerById(event:getCostData(self))
        room:setPlayerMark(player, "@@joy__jieying_camp", 0)
        room:addPlayerMark(tar, "@@joy__jieying_camp")
      else
        room:setPlayerMark(target, "@@joy__jieying_camp", 0)
        if not target:isKongcheng() then
          local dummy = Fk:cloneCard("dilu")
          dummy:addSubcards(target.player_cards[Player.Hand])
          room:obtainCard(player, dummy, false, fk.ReasonPrey)
        end
      end
    end
    return false
  end,
})
gn_jieying:addEffect(fk.TurnStart, {
  name = "joy__gn_jieying",
  anim_type = "drawcard",
  events = {fk.DrawNCards, fk.EventPhaseStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(gn_jieying.name) then return false end
    if event == fk.TurnStart then
      return player == target and table.every(player.room.alive_players, function (p)
        return p:getMark("@@joy__jieying_camp") == 0 end)
    elseif event == fk.EventPhaseStart and target.phase ~= Player.Finish then
      return false
    end
    return target:getMark("@@joy__jieying_camp") > 0
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart and player == target then
      local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function (p)
        return p.id end), 1, 1, "#joy__gn_jieying-choose", gn_jieying.name, true)
      if #to > 0 then
        event:setCostData(self, to[1])
        return true
      end
      return false
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:addPlayerMark(player, "@@joy__jieying_camp")
    elseif event == fk.DrawNCards then
      data.n = data.n + 1
    elseif event == fk.EventPhaseStart then
      if player == target then
        local tar = room:getPlayerById(event:getCostData(self))
        room:setPlayerMark(player, "@@joy__jieying_camp", 0)
        room:addPlayerMark(tar, "@@joy__jieying_camp")
      else
        room:setPlayerMark(target, "@@joy__jieying_camp", 0)
        if not target:isKongcheng() then
          local dummy = Fk:cloneCard("dilu")
          dummy:addSubcards(target.player_cards[Player.Hand])
          room:obtainCard(player, dummy, false, fk.ReasonPrey)
        end
      end
    end
    return false
  end,
})
gn_jieying:addEffect(fk.BuryVictim, {
  refresh_events = {fk.BuryVictim, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    return (event == fk.BuryVictim or data == self) and player:getMark("@@joy__jieying_camp") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if table.every(room.alive_players, function (p) return not p:hasSkill(gn_jieying.name, true) end) then
      room:setPlayerMark(player, "@@joy__jieying_camp", 0)
    end
  end,
})
gn_jieying:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.BuryVictim, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    return (event == fk.BuryVictim or data == self) and player:getMark("@@joy__jieying_camp") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if table.every(room.alive_players, function (p) return not p:hasSkill(gn_jieying.name, true) end) then
      room:setPlayerMark(player, "@@joy__jieying_camp", 0)
    end
  end,
})

gn_jieying:addEffect("targetmod", {
  name = "#joy__gn_jieying_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("@@joy__jieying_camp") > 0 and scope == Player.HistoryPhase then
      return #table.filter(Fk:currentRoom().alive_players, function (p) return p:hasSkill(gn_jieying.name) end)
    end
  end,
})

gn_jieying:addEffect("maxcards", {
  name = "#joy__gn_jieying_maxcards",
  correct_func = function(self, player)
    if player:getMark("@@joy__jieying_camp") > 0 then
      return #table.filter(Fk:currentRoom().alive_players, function (p) return p:hasSkill(gn_jieying.name) end)
    else
      return 0
    end
  end,
})

return gn_jieying